package com.coffee.takeoutserver.controller;

import com.coffee.takeoutserver.bean.GoodsInfo;
import com.coffee.takeoutserver.service.GoodsInfoService;
import com.coffee.takeoutserver.util.RequestType;
import com.coffee.takeoutserver.util.ResponseInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsInfoController {

    @Autowired
    GoodsInfoService goodsInfoService;

    @RequestMapping("getGoodsInfoListBySellerId")
    @ResponseBody
    public String getGoodsInfoListBySellerId(int sellerId) {
        Gson gson = new Gson();
        List<GoodsInfo> goodsInfoList = goodsInfoService.getGoodsInfoListBySellerId(sellerId);
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.GetGoodsInfoListBySellerId );
        map.put("goodsInfoList",gson.toJson(goodsInfoList));
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        responseInfo.setData(gson.toJson(map));;
        return gson.toJson(responseInfo);
    }

    @RequestMapping("deleteGoodsInfoById")
    @ResponseBody
    public String deleteGoodsInfoById(int id) {
        Gson gson = new Gson();
        goodsInfoService.deleteGoodsInfoById(id);
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.DeleteGoodsInfoById );
        map.put("info","success");
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        responseInfo.setData(gson.toJson(map));;
        return gson.toJson(responseInfo);
    }

    @RequestMapping("insertGoodsInfo")
    @ResponseBody
    public String insertGoodsInfo(@RequestBody GoodsInfo goodsInfo) {
        Gson gson = new Gson();
        goodsInfoService.insertGoodsInfo(goodsInfo);
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.InsertGoodsInfo );
        map.put("goodsInfo",gson.toJson(goodsInfo));
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        responseInfo.setData(gson.toJson(map));;
        return gson.toJson(responseInfo);
    }

    @RequestMapping("updateGoodsInfoById")
    @ResponseBody
    public String updateGoodsInfoById(@RequestBody GoodsInfo goodsInfo) {
        Gson gson = new Gson();
        goodsInfoService.updateGoodsInfoById(goodsInfo);
        Map<String,String> map = new HashMap<>();
        map.put("requestType", RequestType.UpdateGoodsInfoById );
        map.put("goodsInfo",gson.toJson(goodsInfo));
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        responseInfo.setData(gson.toJson(map));;
        return gson.toJson(responseInfo);
    }
}
